package com.hengyue.system.service.impl;

import com.hengyue.common.util.DictUtils;
import com.hengyue.common.utils.StringUtils;
import com.hengyue.system.domain.criteria.DictDataCriteria;
import com.hengyue.system.domain.entity.SysDictData;
import com.hengyue.system.repository.SysDictDataRepository;
import com.hengyue.system.service.ISysDictDataService;
import jakarta.persistence.criteria.Predicate;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class SysDictDataServiceImpl implements ISysDictDataService {

    private final SysDictDataRepository dictDataRepository;

    @Override
    public Page<SysDictData> selectDictDataList(DictDataCriteria dictData, Pageable pageable) {
        return dictDataRepository.findAll((root, query, cb) -> {
            List<Predicate> predicates = new ArrayList<>();

            if (StringUtils.isNotEmpty(dictData.getDictLabel())) {
                predicates.add(cb.like(root.get("dictLabel"), "%" + dictData.getDictLabel() + "%"));
            }
            if (StringUtils.isNotEmpty(dictData.getDictType())) {
                predicates.add(cb.equal(root.get("dictType"), dictData.getDictType()));
            }
            if (StringUtils.isNotEmpty(dictData.getStatus())) {
                predicates.add(cb.equal(root.get("status"), dictData.getStatus()));
            }

            return cb.and(predicates.toArray(new Predicate[0]));
        }, pageable);
    }

    @Override
    public String selectDictLabel(String dictType, String dictValue) {
        return dictDataRepository.findByDictTypeAndDictValue(dictType, dictValue);
    }

    @Override
    public List<SysDictData> selectDictDataByType(String dictType) {
        return dictDataRepository.findByDictType(dictType);
    }

    @Override
    public SysDictData selectDictDataById(Long id) {
        return dictDataRepository.findById(id).orElse(null);
    }

    @Override
    @Transactional
    public void deleteDictDataByIds(Long[] ids) {
        // 1. 批量获取所有要删除的字典数据
        List<SysDictData> dictDataList = dictDataRepository.findAllById(Arrays.asList(ids));

        // 2. 按字典类型分组
        Map<String, List<SysDictData>> dictTypeMap = dictDataList.stream()
                .collect(Collectors.groupingBy(SysDictData::getDictType));

        // 3. 批量删除
        dictDataRepository.deleteAllByIdInBatch(Arrays.asList(ids));

        // 4. 按字典类型批量更新缓存
        dictTypeMap.forEach((dictType, dataList) -> {
            List<SysDictData> remainingData = dictDataRepository.findByDictType(dictType);
            DictUtils.setDictCache(dictType, remainingData);
        });
    }

    @Override
    @Transactional
    public void insertDictData(SysDictData dictData) {
        dictDataRepository.save(dictData);
        List<SysDictData> dataList = dictDataRepository.findByDictType(dictData.getDictType());
        DictUtils.setDictCache(dictData.getDictType(), dataList);
    }

    @Override
    @Transactional
    public void updateDictData(SysDictData dictData) {
        dictDataRepository.save(dictData);
        List<SysDictData> dataList = dictDataRepository.findByDictType(dictData.getDictType());
    }
}

